package cn.jinjimi.app.main.widget.RollViewPager;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;

import cn.jinjimi.app.R;

/**
 * Created by Roy
 * Date: 15/11/16
 */
public class PointHintView extends LinearLayout implements HintView {
    private ImageView[] mDots;
    private int length = 0;
    private int lastPosition = 0;

    public PointHintView(Context context) {
        super(context);
    }

    public PointHintView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public void initView(int length, int gravity) {
        removeAllViews();
        setOrientation(HORIZONTAL);
        switch (gravity) {
            case 0:
                setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
                break;
            case 1:
                setGravity(Gravity.CENTER);
                break;
            case 2:
                setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
                break;
        }

        this.length = length;
        mDots = new ImageView[length];

        for (int i = 0; i < length; i++) {
            mDots[i] = new ImageView(getContext());
            LayoutParams dotlp = new LayoutParams(
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);
            dotlp.setMargins(getContext().getResources().getDimensionPixelOffset(R.dimen.dp_5), 0, 0, 0);
            mDots[i].setLayoutParams(dotlp);
            mDots[i].setBackgroundResource(R.drawable.selector_point_icon);
            addView(mDots[i]);
        }

        setCurrent(0);
    }

    @Override
    public void setCurrent(int current) {
        if (current < 0 || current > length - 1) {
            return;
        }
        if (lastPosition < length)
            mDots[lastPosition].setSelected(false);
        mDots[current].setSelected(true);
        lastPosition = current;
    }
}
